﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveSequence.Interface;
using LiveSequence.Interface;
using LiveSequence.Suite.Interfaces;
using System.Drawing;

namespace LiveSequence.Suite.Views
{

  public class BaseView<T> : UserControl, IView where T : class, IDocument 
  {

    private string _strTitle;
    private static Color _outRectangleColor;
    private static event EventHandler OnChangeStyle;
    public event EventHandler SelectedDocumentChanged;
    protected IDocument _document;
    protected ToolStrip _toolStrip;

    protected bool PaintOutRectangle
    {
      get;
      set;
    }

    public T SelectedDocument
    {
      get { return _document as T; }
      set
      {
        if(_document != null)
        {
          _document.UpdateView -= _document_UpdateView;
        }
        _document = value;
        if (_document != null)
        {
          _document.UpdateView += _document_UpdateView;
        }
        OnSelectedDocumentChanged();

      }
    }

    void _document_UpdateView(object sender, EventArgs e)
    {
      UpdateView(null);
    }

    protected virtual void OnSelectedDocumentChanged()
    {
      if(SelectedDocumentChanged!= null)
      {
        SelectedDocumentChanged(this, EventArgs.Empty);
      }
    }

    public static Color OutRectangleColor
    {
      get { return _outRectangleColor; }
      set
      {
        _outRectangleColor = value;
        if (OnChangeStyle != null)
        {
          OnChangeStyle(null, EventArgs.Empty);
        }
      }
    }

    public BaseView()
    {
      PaintOutRectangle = true;
      OutRectangleColor = Color.Blue;
      OnChangeStyle += new EventHandler(BaseView_OnChangeStyle);

      _toolStrip = null;
    }

    void BaseView_OnChangeStyle(object sender, EventArgs e)
    {
      Invalidate();
    }


    #region IView Members

    public virtual System.Windows.Forms.Control Control
    {
      get { return this; }
    }

    public virtual Image Icon
    {
      get { return null; }
    }

    public virtual string Title
    {
      get
      {
        return _strTitle;
      }
      set
      {
        _strTitle = value;
      }
    }

    public virtual System.Windows.Forms.ToolStrip ViewToolStrip
    {
      get { return _toolStrip; }
    }

    public virtual System.Windows.Forms.MenuStrip ViewMenuStrip
    {
      get { return null; }
    }

    public virtual System.Windows.Forms.StatusStrip ViewStatusStrip
    {
      get { return null; }
    }

    public virtual System.Windows.Forms.ContextMenuStrip ViewContextMenuStrip
    {
      get { return null; }
    }

    public virtual void UpdateView(object obj)
    {

    }

    public virtual void OnActivateView(bool bActivate)
    {
      throw new Exception("The method or operation is not implemented.");
    }

    #endregion

    #region IView Members



    #endregion

    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      if (PaintOutRectangle)
      {

        Graphics g = e.Graphics;
        using (Pen p = new Pen(OutRectangleColor))
        {
          g.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
        }
      }

    }

    #region IView Members


    public IDocument Document
    {
      get
      {
        return _document;
      }
      set
      {
        _document = value;
        OnDocumentChanged();
      }
    }

    public void OnDocumentChanged()
    {
      
    }

    #endregion
  }

}
